﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class FormJiesuan : Form
    {
        public FormJiesuan()
        {
            InitializeComponent();
        }

        private void FormJiesuan_Load(object sender, EventArgs e)
        {
           // comboBox报工人员Initl();
        }

        //private void comboBox报工人员Initl()
        //{
        //    string sql;
        //    sql = "select  cUser_Name from Z_UA_User where  bCCUser is not  null and cdept !='系统组' order by cUser_Name ";

        //    DataTable tb = pub.SysGetTable(sql);
        //    DataRow dr = tb.NewRow();
        //    dr["cUser_Name"] = "请选择";
        //    tb.Rows.InsertAt(dr, 0);
        //    comboBox报工人员.DataSource = tb;

        //    comboBox报工人员.DisplayMember = "cUser_Name";
        //}

        private void button刷新_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("006001"))
            {
                List报工记录();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
           
        }

        private void List报工记录()
        {
            string sql;
            ///最大顺序号的报工单，未入库的
            sql = @"    select '' 行号, id,生产订单号, 生产订单行号, 物料编号,inventory.cinvname 物料名称, 订单数量, 加工顺序, 工序编号, 工序名称, 报工数量, 工序单价, 
                开始时间, 结束时间, 报工工时, 工时单价, 金额,结算金额, 报工人员,Z_UA_User.cuser_name 报工人员名称, 报工时间, 是否工时结算,'' 是否已检验,'' 入库单号,''是否关闭,是否已经计算
                FROM      Z_baogong join Z_UA_User  on Z_UA_User.cUSER_Id=z_baogong.报工人员 join inventory on inventory.cinvcode=Z_baogong.物料编号 where 1=1 ";

            if (this.textBox报工人员.Text != "")
            {
                sql += " and cuser_name like '%" + this.textBox报工人员.Text + "%'";
            }

            if (this.dateTimePicker_ksrq.Checked)
            {
                sql += " and 报工时间>='" + this.dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (this.dateTimePicker_jsrq.Checked)
            {
                sql += " and 报工时间<'" + this.dateTimePicker_jsrq.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            }
            if (!this.checkBox包含已计算.Checked)
            {
                sql += " and 是否已经计算='未结算'";
            }

            sql += " order by  id asc ";
            DataTable tb = pub.U8GetTable(sql);
            this.dataGridView1.AutoGenerateColumns = false;

            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            if(tb!=null){
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                tb.Rows[i]["行号"] = (i + 1).ToString();
            }
            }

            this.dataGridView1.DataSource = tb;
            this.dataGridView1.Refresh();
            for (int j = 0; j < dataGridView1.Rows.Count;j++ )
            {
                if (this.dataGridView1.Rows[j].Cells["C是否已经计算"].Value.ToString() == "已结算")
                {
                    this.dataGridView1.Rows[j].Cells["C结算金额"].ReadOnly = true;
                }
                else
                {
                    this.dataGridView1.Rows[j].Cells["C结算金额"].ReadOnly =false;
                    this.dataGridView1.Rows[j].Cells["C结算金额"].Style.BackColor = Color.Green;
                }
            }
                
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            ksrow = e.RowIndex;
            this.checkBox选择打钩.Checked = false;
        }

        public int ksrow { get; set; }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            jsrow = e.RowIndex;
            this.checkBox选择打钩.Checked = false;
        }

        public int jsrow { get; set; }

        private void checkBox选择打钩_CheckedChanged(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;
            if (ksrow > jsrow)
            {
                int r;
                r = ksrow;
                ksrow = jsrow;
                jsrow = r;

            }
            for (int i = ksrow; i <= jsrow; i++)
            {
                this.dataGridView1.Rows[i].Cells[1].Value = checkBox选择打钩.Checked;
                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }
            this.progressBar1.Value = 100;
        }

        private void checkBox全部打钩_CheckedChanged(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;

            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                this.dataGridView1.Rows[i].Cells[1].Value = checkBox全部打钩.Checked;
                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }
            this.progressBar1.Value = 100;
        }

        private bool IsCal(int 报工ID)
        {
            string sql;
            sql = "select * from Z_baogong where id='" + 报工ID + "' and 是否已经计算='已结算' ";
            DataTable tb = pub.U8GetTable(sql);
            if (tb.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void button结算_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("006002"))
            {
                ///如果未结算，则进行计算
                string sql;
                int id;
                decimal 结算金额;
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    if (this.dataGridView1.Rows[i].Cells["C选择"].Value != null)
                    {
                        if ((bool)this.dataGridView1.Rows[i].Cells["C选择"].Value == true)
                        {
                            //
                            id = int.Parse(this.dataGridView1.Rows[i].Cells["Cid"].Value.ToString());

                            if (!IsCal(id))
                            {
                                ///未被计算过
                                结算金额 = pub.ToDecimal(this.dataGridView1.Rows[i].Cells["C结算金额"].Value.ToString());
                                if (结算金额 == 0)
                                {
                                    this.dataGridView1.Rows[i].Cells["C结算金额"].Value = this.dataGridView1.Rows[i].Cells["C金额"].Value;
                                    结算金额 = pub.ToDecimal(this.dataGridView1.Rows[i].Cells["C结算金额"].Value.ToString());
                                    this.dataGridView1.Rows[i].Cells["C结算金额"].ReadOnly = true;
                                }
                                else
                                {
                                    ///有手填了
                                }
                                ///更新报工表的结算金额和是否已经结算
                                sql = "update   Z_baogong set 是否已经计算='已结算' , 结算金额='" + 结算金额 + "' where id='" + id + "' ";
                                pub.ExcuteSql(sql, "计算计件计时工资");
                            }
                            else
                            {
                                //已经被计算了，则从数据库中取值
                                this.dataGridView1.Rows[i].Cells["C结算金额"].Value = Get结算金额(id);
                            }
                        }
                    }

                }
                ///重新查询
                List报工记录();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }

           
        }

        private decimal Get结算金额(int id)
        {
            string sql;
            sql = "select 结算金额 from Z_baogong where id='" + id + "' ";
            DataTable tb = pub.U8GetTable(sql);
            if (tb.Rows.Count >0)
            {
                return pub.ToDecimal(tb.Rows[0][0].ToString());
            }
            else
            {
                return 0;
            }

        }

        private void button取消确认_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("006003"))
            {
                //取消确认
                string sql;
                int id;
                decimal 结算金额;
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    if (this.dataGridView1.Rows[i].Cells["C选择"].Value != null)
                    {
                        if ((bool)this.dataGridView1.Rows[i].Cells["C选择"].Value == true)
                        {

                            id = int.Parse(this.dataGridView1.Rows[i].Cells["Cid"].Value.ToString());

                            if (IsCal(id))
                            {
                                ///已被计算过
                                结算金额 = pub.ToDecimal(this.dataGridView1.Rows[i].Cells["C结算金额"].Value.ToString());
                                if (结算金额 != 0)
                                {
                                    this.dataGridView1.Rows[i].Cells["C结算金额"].Value = 0;
                                }
                                else
                                {

                                }
                                ///更新报工表的结算金额和是否已经结算
                                sql = "update   Z_baogong set 是否已经计算='未结算' , 结算金额='0' where id='" + id + "' ";
                                pub.ExcuteSql(sql, "取消计算计件计时工资");
                            }
                            else
                            {

                            }
                        }
                    }

                }
                ///重新查询
                List报工记录();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
            
          
        }



        private void button2_Click(object sender, EventArgs e)
        {
            //if (pub.是否有权限("006004"))
            //{
               
            //                string fileName = "";
            //                string saveFileName = "";
            //                SaveFileDialog saveDialog = new SaveFileDialog();
            //                saveDialog.DefaultExt = "xls";
            //                saveDialog.Filter = "Excel文件|*.xls";
            //                saveDialog.FileName = fileName;
            //                saveDialog.ShowDialog();
            //                saveFileName = saveDialog.FileName;
            //                if (saveFileName.IndexOf(":") < 0) return; //被点了取消
            //                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            //                if (xlApp == null)
            //                {
            //                    MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");
            //                    return;
            //                }
            //                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            //                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            //                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
            //                //写入标题             
            //                for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //                { worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; }
            //                //写入数值
            //                for (int r = 0; r < dataGridView1.Rows.Count; r++)
            //                {
            //                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //                    {
            //                        worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
            //                    }
            //                    System.Windows.Forms.Application.DoEvents();
            //                }
            //                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
            //                MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);
            //                if (saveFileName != "")
            //                {
            //                    try
            //                    {
            //                        workbook.Saved = true;
            //                        workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 
            //                    }
            //                    catch (Exception ex)
            //                    {//fileSaved = false;                      
            //                        MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);
            //                    }
            //                }
            //                xlApp.Quit();
            //                GC.Collect();//强行销毁           }
            //            }
            //            else
            //            {
            //                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            //            }
                    }
                }
            }
        